package com.mohan.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.mohan.gulimall.product.entity.BrandEntity;
import com.mohan.gulimall.product.entity.CategoryEntity;
import com.mohan.gulimall.product.service.BrandService;
import com.mohan.gulimall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mohan.common.utils.PageUtils;
import com.mohan.common.utils.Query;

import com.mohan.gulimall.product.dao.CategoryBrandRelationDao;
import com.mohan.gulimall.product.entity.CategoryBrandRelationEntity;
import com.mohan.gulimall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {
  @Autowired
  private BrandService brandService;
  @Autowired
  private CategoryService categoryService;

  @Override
  public PageUtils queryPage(Map<String, Object> params) {
    IPage<CategoryBrandRelationEntity> page = this.page(
            new Query<CategoryBrandRelationEntity>().getPage(params),
            new QueryWrapper<CategoryBrandRelationEntity>()
    );

    return new PageUtils(page);
  }

  @Override
  public void saveAllData(CategoryBrandRelationEntity categoryBrandRelation) {
    BrandEntity brandEntity = brandService.getById(categoryBrandRelation.getBrandId());
    CategoryEntity categoryEntity = categoryService.getById(categoryBrandRelation.getCatelogId());
    categoryBrandRelation.setBrandName(brandEntity.getName());
    categoryBrandRelation.setCatelogName(categoryEntity.getName());
    this.save(categoryBrandRelation);
  }

  @Override
  public void updateBrand(Long brandId, String name) {
    CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
    categoryBrandRelationEntity.setBrandId(brandId);
    categoryBrandRelationEntity.setBrandName(name);
    UpdateWrapper<CategoryBrandRelationEntity> wrapper = new UpdateWrapper<>();
    wrapper.eq("brand_id",brandId);
    this.update(categoryBrandRelationEntity,wrapper);
  }

  @Override
  public void updateCategory(Long catId, String name) {
    baseMapper.updateCategory(catId,name);
  }

}